package world;

public class Chunk {
	public final static int SIZE = 16;
	public final static int LAYER_SIZE = 16;
	private int indexX, indexZ;
	private World world; 
	private float baseX, baseY, baseZ;
	private ChunkLayer[] layerArray;

	public Chunk(World world, int indexX, int indexZ) {
		this.world = world;
		this.indexX = indexX;
		this.indexZ = indexZ;
		this.baseX = indexX * SIZE;
		this.baseZ = indexZ * SIZE;
		this.baseY = 0;
		this.layerArray = new ChunkLayer[LAYER_SIZE];
		layerArray[0] = new ChunkLayer(world, indexX, 0, indexZ);
	}
	
	public void render() {
		for(ChunkLayer layer: layerArray){
			if(layer==null){
				continue;
			}
			
			layer.render();
		}		
	}
	
	public static int getID(int indexX, int indexZ) {
		return (indexX << 16) + indexZ;
	}

	public  int getID() {
		return (indexX << 16) + indexZ;
	}
	
	public int getIndexX() {
		return indexX;
	}

	public int getIndexZ() {
		return indexZ;
	}
	
	public void setBlocks(ChunkBlock[][][] blocks) {
		layerArray[0].setBlocks(blocks);		
	}
	
	public void buildRenderData() {
		for(ChunkLayer layer: layerArray){
			if(layer==null){
				continue;
			}
			
			layer.buildRenderData();
		}	
	}

	public ChunkBlock findBlock(int posInChunkX, int posInChunkY, int posInChunkZ) {
		int layerIndex  =  posInChunkY>>4;
		if(layerIndex>layerArray.length ){
			return null;
		}
		
		ChunkLayer layer = layerArray[layerIndex];
		if(layer==null){
			return null;	
		}else{
			return layer.findBlock(posInChunkX, posInChunkY, posInChunkZ);
		}
	}
}

